package com.woniuxy.jiapei.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.jiapei.pojo.Role;
import com.woniuxy.jiapei.service.RoleService;
import com.woniuxy.jiapei.service.UserRoleService;
import com.woniuxy.jiapei.service.UserService;
import com.woniuxy.jiapei.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.jiapei.pojo.Shareholder;
import com.woniuxy.jiapei.pojo.User;
import com.woniuxy.jiapei.service.ShareholderService;
import com.woniuxy.jiapei.vo.ShareholdersQueryVo;
import com.woniuxy.jiapei.vo.ShareholdersVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.ArrayList;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lr
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/jiapei/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ShareholderService shareholderService;

    public R getShareHolders() {
        QueryWrapper roleQueryWrapper = new QueryWrapper();
        List<String> names = new ArrayList<>();
        names.add("股东");
        names.add("一级分销商");
        names.add("二级分销商");
        List<Role> roles = roleService.list(roleQueryWrapper);
        List<Integer> userIds = new ArrayList<>();
        return null ;
    }

    @ApiOperation("查询股东列表信息")
    @GetMapping("/shareholders")
    public R shareholdersList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "limit", defaultValue = "5") Integer limit,
                              ShareholdersQueryVo vo) {
        Page<ShareholdersVo> page1 = new Page<>(page, limit);
        IPage<ShareholdersVo> userIPage = userService.selectShareholderList(page1, vo);
        long total = userIPage.getTotal();
        List<ShareholdersVo> records = userIPage.getRecords();

        return R.ok().data("total", total).data("list", records);
    }
    @ApiOperation("按条件导出股东列表信息excel")
    @GetMapping("/shareholders/excel")
    public R shareholdersListExcel(ShareholdersQueryVo vo) {
        List<ShareholdersVo> list = userService.selectShareholderListExcel(vo);
        return R.ok().data("list",list);
    }
    @ApiOperation("新增股东信息")
    @PostMapping("/shareholders")
    public R addShareholders(@RequestBody ShareholdersVo vo) {
        userService.addShareholder(vo);
        return R.ok();
    }
    @ApiOperation("修改股东信息")
    @PutMapping("/shareholders")
    public R updateShareholders(@RequestBody ShareholdersVo vo) {
        userService.updateShareholder(vo);
        return R.ok();
    }
    @ApiOperation("新增时验证手机号码是否存在")
    @GetMapping("/checkPhone")
    public R checkName(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        User user = userService.getOne(wrapper);
        if (user == null) {
            return R.ok();
        }
        return R.error();
    }
    @ApiOperation("修改时验证手机号码是否存在")
    @GetMapping("/checkPhone2")
    public R upCheckName(String phone,Integer id) {
        User user1 = userService.getById(id);
        if (user1.getPhone().equals(phone)) {
            return R.ok();
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        User user = userService.getOne(wrapper);
        if (user == null) {
            return R.ok();
        }
        return R.error();
    }
    @ApiOperation("根据id删除用户")
    @DeleteMapping("/shareholders/{id}")
    public R deleteShareholder(@PathVariable Integer id){
        userService.deleteShareholder(id);
    return R.ok();
    }
    @ApiOperation("根据id查询用户")
    @GetMapping("/shareholders/{id}")
    public R shareholderInfo(@PathVariable Integer id){
        User user = userService.getById(id);
        QueryWrapper<Shareholder> wrapper = new QueryWrapper<>();
        wrapper.eq("use_id",id);
        Shareholder shareholder = shareholderService.getOne(wrapper);
        ShareholdersVo vo = new ShareholdersVo();
        vo.setId(id);
        vo.setName(user.getName());
        vo.setPhone(user.getPhone());
        vo.setWxName(user.getWxName());
        vo.setEmail(user.getEmail());
        vo.setCreatetime(user.getCreatetime());
        vo.setInvestmentamount(shareholder.getInvestmentamount());
        vo.setShare(shareholder.getShare());
        vo.setCardnumber(shareholder.getCardnumber());
        vo.setEntrytime(shareholder.getEntrytime());
        return R.ok().data("vo",vo);
    }
}




